#ifndef __HTTP_CONN_H__
#define __HTTP_CONN_H__

#include <sys/uio.h>
#include <arpa/inet.h>
#include <string>
#include "../buffer/buffer.h"
#include "httprequest.h"
#include "httpresponse.h"
#include "glog/logging.h"
#include <atomic>

class HttpConn {
public:
    HttpConn();
    ~HttpConn();

    void init(int fd, sockaddr_in addr);

    void Close();

    std::string GetIP();
    int GetPort();
    int GetFd();
    sockaddr_in GetAddr();

    ssize_t Read(int *Errno);
    ssize_t Write(int *Errno);
    bool Process();

    bool IsKeepAlive() {
        return req.IsKeepAlive();
    }


    int ToWriteByets() {
        return iov_[0].iov_len+iov_[1].iov_len;
    }

    static std::atomic<int> userCount;
    static bool isET;
    static const char* srcDir;

private:
    int fd_;
    sockaddr_in addr_;

    Buffer readBuffer_;
    Buffer writeBuffer_;

    int iovCnt_;
    iovec iov_[2];

    bool isClose_;

    HttpRequest req;
    HttpResponse res;
};

#endif /* __HTTP_CONN_H__ */
